package com.spark.tags;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.spark.core.SparkBuilder;
import com.spark.core.SparkContext;
import com.spark.core.XMap;
import com.spark.util.IDGenerator;

@SuppressWarnings("unchecked")
public class Menu extends ViewComponent {
	
	public static final String TAG_NAME = "menu";
	
	public String PROPERTITY_TEXT = "text";
	public String PROPERTITY_ROLE = "role";
	public String PROPERTITY_MENU_ID = "mid";
	public String PROPERTITY_ICON_CLASS = "iconClass";
	public String PROPERTITY_ALIGN = "align";
	
	
	public static final String ROLE_MENUITEM = "menuitem";
	public static final String ROLE_SEPARATOR = "separator";
	public static final String ROLE_OBJECT = "object";
//	public static final String ROLE_OBJECT = "object";
	
	private static final String DEFAULT_MENU_CLASSNAME = "s-menu";	
	private static final String DEFAULT_TEMPLATES = "menu.ftl";
	private static final String MENU_ITMES = "items";
	private static final String MENU_MENU_MAP = "map";
	
	public String getTemplate(){
		return DEFAULT_TEMPLATES;
	}
	
	protected String getDefaultClass(XMap view,SparkContext context){
		return DEFAULT_MENU_CLASSNAME;
	}
	
	protected void filterJsonConfig(SparkContext context){
		context.removeJsonConfig(PROPERTITY_WIDTH);
		context.removeJsonConfig(PROPERTITY_HEIGHT);
	}
	
	public void init(XMap view, SparkContext context) throws Exception {
		super.init(view, context);
		List<String> menus = new ArrayList<String>();
		context.put(MENU_ITMES,menus);
		Map<String,String> mmap = new HashMap<String,String>();
		context.put(MENU_MENU_MAP,mmap);
		XMap items = view.getChild(MENU_ITMES);
		if(items!=null){
			context.openIdTrace();
			String id = (String)context.get(PROPERTITY_ID);
			createMenu(items,id,context);
			StringBuilder sb = new StringBuilder();
			for(String menuStr:menus){
				sb.append(menuStr);
			}
			List idList = context.getIdList();
			context.closeIdTrace();
			context.put(MENU_ITMES, sb.toString());
			context.addJsonConfig("child", idList);
			context.addJsonConfig(PROPERTITY_ALIGN,view.getString(PROPERTITY_ALIGN,"r"));
			context.addJsonConfig(MENU_MENU_MAP, mmap);	
		}
	}
	
	
	
	private void createMenu(XMap menu,String id, SparkContext context) throws Exception{
		List<XMap> list = menu.getChilds();
		if(list != null){
			StringBuilder sb = new StringBuilder();			
			sb.append("<div class=\"s-menu\" role=\"menu\" hideFocus unselectable=\"on\" id=\""+id+"\">");
			Iterator<XMap> it = list.iterator();
			while(it.hasNext()){
				XMap item = it.next();
				String role = item.getString(PROPERTITY_ROLE);
				if(ROLE_MENUITEM.equals(role)){
					sb.append(createMenuItem(item,context));
				}else if(ROLE_SEPARATOR.equals(role)){
					sb.append(createMenuSeparator(item,context));
				}else if(ROLE_OBJECT.equals(role)){
					sb.append(createMenuObject(item,context));
				}
			}
			sb.append("</div>");
			List menus = (List)context.get(MENU_ITMES);	
			menus.add(sb.toString());
		}
	}
	
	private String createMenuItem(XMap item,SparkContext context) throws Exception {
		List<XMap> children = item.getChilds();		
		StringBuilder sb = new StringBuilder();
		String text = item.getString(PROPERTITY_TEXT,"");
		String mid = item.getString(PROPERTITY_MENU_ID,"");
		String iconCls = item.getString(PROPERTITY_ICON_CLASS,"");
		String style = item.getString(PROPERTITY_STYLE,"");
		String cls = item.getString(PROPERTITY_CLASSNAME,"");
		
		sb.append("<div class=\"s-menuitem ").append(cls).append("\" role=\"menuitem\"  mid=\"").append(mid).append("\"");
		if(!"".equals(style)){
			sb.append(" style=\"").append(style).append("\"");
			
		}
		sb.append(">");
	    sb.append("<div class=\"s-menuitem-content\">");
	    if("".equals(iconCls)){
	    	sb.append(text);	    	
	    }else {
	    	sb.append("<div class=\"s-menu-icon\">");
	    	sb.append("<div class=\"").append(iconCls).append("\"></div></div>");
	    	sb.append("<span>").append(text).append("</span>");
	    }
	    if(children!=null&&children.size()>0){
	    	sb.append("<span class=\"s-submenu-arrow\">►</span>");
	    	String id = IDGenerator.getInstance().generate();
	    	if(!"".equals(mid)){
	    		Map<String,String> map = (Map<String,String>)context.get(MENU_MENU_MAP);
	    		map.put(mid, id);
	    	}
	    	createMenu(item,id,context);
	    }
	    sb.append("</div></div>");
	    
		return sb.toString();
	}
	
	
	private String createMenuSeparator(XMap view,SparkContext context){
		String style = view.getString(PROPERTITY_STYLE,"");
		StringBuilder sb = new StringBuilder();
		String cls = view.getString(PROPERTITY_CLASSNAME,"");
		sb.append("<div class=\"s-menuseparator ").append(cls).append("\"  role=\"separator\" mid=\"").append(view.getString(PROPERTITY_MENU_ID,"")).append("\"");
		if(!"".equals(style)){
			sb.append(" style=\"").append(style).append("\"");
			
		}
		sb.append("></div>");
		return sb.toString();
	}
	
	private String createMenuObject(XMap view,SparkContext context) throws Exception{
		String style = view.getString(PROPERTITY_STYLE,"");
		StringBuilder sb = new StringBuilder();
		String cls = view.getString(PROPERTITY_CLASSNAME,"");
		sb.append("<div class=\"s-menuitem-object ").append(cls).append("\"  role=\"separator\" mid=\"").append(view.getString(PROPERTITY_MENU_ID,"")).append("\"");
		if(!"".equals(style)){
			sb.append(" style=\"").append(style).append("\"");			
		}
		sb.append(">");
		for(XMap item:view.getChilds()) {
			sb.append(SparkBuilder.build(item, context));
		}
		sb.append("</div>");
		return sb.toString();
	}
	
	
	
	protected String build(SparkContext context){
		Map map = context.getMap();
		StringBuilder sb = new StringBuilder();
		sb.append(get(map,"items"));
		sb.append("<script>new Spark.Menu(").append(get(map,"config")).append(");</script>");
		return sb.toString();
	}
}
